<?php
/*
Plugin Name: Team Desk vs Gravity Form
Plugin URI: http://wpcreative.info
Description: A brief description of the Plugin.
Version: 1.0
Author: wpcreative
Author URI: http://wpcreative.info
*/
require_once("TeamDesk/API.php");
//include("TeamDesk/Dataset.php");

/* Hook wpcf7 plugin */
add_action( 'wpcf7_before_send_mail', 'tdapi_send_data_to_teamdesk' );

function tdapi_send_data_to_teamdesk($cf7_data){
		//print_r($cf7_data);
		/*
		* Sample data
		
		[posted_data] => Array
			(
				[_wpcf7] => 4
				[_wpcf7_version] => 3.1.2
				[_wpcf7_unit_tag] => wpcf7-f4-p2-o1
				[_wpnonce] => cb0c4f728d
				[your-name] => tt
				[your-email] => tt@xx.com
				[your-subject] => tt
				[your-message] => t
				[_wpcf7_is_ajax_call] => 1
			)
		*/
		
	//$url = "http://localhost/wordpress-mu/sample-page/";
	//echo file_get_contents($url, FILE_TEXT, stream_context_create(array('http' => array('timeout' => 1))), 0, 1);  
	$tdapi_name = $cf7_data->posted_data['your-name'];
	$tdapi_email = $cf7_data->posted_data['your-email'];
	$tdapi_subject = $cf7_data->posted_data['your-subject'];
	$tdapi_message = $cf7_data->posted_data['your-message'];
	
	$tdObj = new TeamDeskAPI(23751);
	$r = $tdObj->login("lehoaian.it@gmail.com", "Help1234desk");
	//print_r($tdObj);
	//$r = $tdObj->DescribeTable("contact");
	//print_r($tdObj->DescribeApp());
	//print_r($tdObj->GetUserInfo());
	
	$ds = new DataSet(array(
		"name" => TeamDeskAPI::Text,
		"email" => TeamDeskAPI::Text,
		"description" => TeamDeskAPI::Text
	));
	
	$ds->AddColumn("Timestamp", TeamDeskAPI::Timestamp);
	
	// print_r($ds);
	$record = array (
		"name" 			=> $tdapi_name,
		"email" 		=> $tdapi_email,
		"description" 	=> $tdapi_message
	);
	

	
	// add new row to a dataset
	array_push($ds->Rows, $record);
	//print_r($ds);
	//print_r($ds->toXML());		
	try {	
		// Submit to TeamDesk
		//print_r($tdObj->Query("SELECT * FROM [contact]"));
		$id = $tdObj->Create("contact", $ds);
		//print_r($id);
		$r = $tdObj->logout();
	}
	catch(Exception $e)
	{
		$tdObj->dumpRequest();
		$tdObj->dumpResponse();
		//print_r($e);
		echo $e->getMessage();
	}
}
?>